/**
 * Date:2021年3月15日 上午10:24:47
 * Copyright (c) 2021, www.bwbroad.com All Rights Reserved
 */

package com.app.rabbit;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * 
 * Description: 参考 https://blog.csdn.net/qq_36357242/article/details/107686056
 * <br/>
 * Date:2021年3月31日 下午3:16:31 <br/>
 * 
 * @author xuejianxin
 */
@Component
public class FanoutProducer {

	private static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(FanoutProducer.class);

	@Autowired
	private RabbitTemplate rabbitTemplate;

	// 申明俩个发布订阅模式的交换机
	@Bean
	public FanoutExchange fanout() {
		return new FanoutExchange("exchange.fanout");
	}

	// 申明俩个发布订阅模式队列
	@Bean
	public Queue fanoutQueue1() {
		return new Queue("fanout1");// 队列一
	}

	@Bean
	public Queue fanoutQueue2() {
		return new Queue("fanout2");// 队列二
	}

	// 将队列一绑定到交换机
	@Bean
	public Binding fanoutBinding1(FanoutExchange fanout, Queue fanoutQueue1) {
		return BindingBuilder.bind(fanoutQueue1).to(fanout);
	}

	// 将队列二绑定到交换机
	@Bean
	public Binding fanoutBinding2(FanoutExchange fanout, Queue fanoutQueue2) {
		return BindingBuilder.bind(fanoutQueue2).to(fanout);
	}

	// 创建消息发送者
	public void sendToFanout(String msg) {
		String context = "发布订阅模式发送的消息:" + msg;
		this.rabbitTemplate.convertAndSend("exchange.fanout", "", context);
	}

}
